<template>
  <view class="content">
    <u-cell-group>
      <u-swipe-action
        v-for="item in sourceList"
        :key="item.id"
        :autoClose="true"
      >
        <u-swipe-action-item
          :options="options2"
          @click="changeOperation"
          :name="item.id"
        >
          <view class="swipe-action u-border-top u-border-bottom">
            <view class="swipe-action__content">
              <u-cell :title="item.name" center></u-cell>
            </view>
          </view>
        </u-swipe-action-item>
      </u-swipe-action>
    </u-cell-group>
    <u-button
      @click="add"
      :customStyle="btnStyle"
      color="#19BC9C"
      type="success"
      >添加</u-button
    >
    <u-modal
      :show="show"
      :content="content"
      :showCancelButton="true"
      @confirm="confirm"
      @cancel="show = false"
    ></u-modal>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
export default {
  data() {
    return {
      btnStyle: {
        width: "100%",
        position: "fixed",
        bottom: "0px",
      },
      sourceList: [],
      LoginUser: {},
      options2: [
        {
          text: "编辑",
          id: "edit",
          style: {
            backgroundColor: "#3c9cff",
          },
        },
        {
          text: "删除",
          id: "del",
          style: {
            backgroundColor: "#f56c6c",
          },
        },
      ],
      show: false,
      content: "确认是否删除该客户来源?",
      id: 0,
    };
  },
  onShow() {
    console.log("正在显示");
    this.getData();
  },
  methods: {
    async getData() {
      let result = await this.$u.api.manage.SourceIndex();

      if (result.code === 1) {
        console.log(result);
        console.log("222");
        this.sourceList = result.data;

        console.log(this.sourceList);
      }
    },
    changeOperation(item) {
      if (item.index === 0) {
        this.$u.route("/pages/manage/source/edit/edit", { id: item.name });
      } else if (item.index === 1) {
        this.show = true;

        this.id = item.name;
      }
    },
    async confirm() {
      this.show = false;

      let result = await this.$u.api.manage.SourceDel({ id: this.id });

      if (result.code === 1) {
        this.$refs.uToast.show({
          type: "success",
          message: result.msg,
          duration: 1500,
          complete: () => {
            this.getData();
          },
        });
      } else {
        this.$refs.uToast.show({
          type: "error",
          message: result.msg,
          duration: 1500,
        });
      }
    },
    add() {
      this.$u.route("/pages/manage/source/add/add");
    },
  },
};
</script>